<!DOCTYPE html>
<html>
<head>
    <title>发药详情</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/style.css"/>
    <script type="text/javascript" src="../../Js/jquery.js"></script>
    <script type="text/javascript" src="../../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../../Js/ckform.js"></script>
    <script type="text/javascript" src="../../Js/common.js"></script>

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">
        $(function () {
            $('#newNav').click(function () {
                window.location.href = "add.html";
            });
        });

        function checkall() {
            var alls = document.getElementsByName("check");
            var ch = document.getElementById("checkall");
            if (ch.checked) {
                for (var i = 0; i < alls.length; i++) {
                    alls[i].checked = true;
                }
            } else {
                for (var i = 0; i < alls.length; i++) {
                    alls[i].checked = false;
                }
            }
        }

        function delAll() {
            var alls = document.getElementsByName("check");
            var ids = new Array();
            for (var i = 0; i < alls.length; i++) {
                if (alls[i].checked) {
                    ids.push(alls[i].value);
                }
            }
            if (ids.length > 0) {
                if (confirm("确认操作?")) {
                    alert("成功!");
                }
            } else {
                alert("请选中要操作的项");
            }
        }

        $(function () {
            $('#backid').click(function () {
                window.location.href = "dispensing.html";
            });
        });
    </script>
</head>
<body>

<table class="table table-bordered table-hover definewidth m10">
    <thead>
    <tr>
        <th>病人号</th>
        <th>姓名</th>
        <th>药品名称</th>
        <th>药品数量</th>
        <th>已发数量</th>
        <th>未发数量</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody id="tbody">

    <tr>
        <!--<td style="vertical-align:middle;" name="behId">1103</td>-->
        <!--<td style="vertical-align:middle;" name="behName">黄飞鸿</td>-->
        <!--<td style="vertical-align:middle;" name="drName">3402-九九感冒灵</td>-->
        <!--<td style="vertical-align:middle;" name="drNumber">3</td>-->
        <!--<td style="vertical-align:middle;" name="drOknumber">2</td>-->
        <!--<td style="vertical-align:middle;" name="notSentDrugNum">1</td>-->
        <!--<td style="vertical-align:middle;">-->
        <!--    <a href="#">发全</a>&nbsp;&nbsp;&nbsp;-->
        <!--    <a href="#">发1</a>&nbsp;&nbsp;&nbsp;-->
        <!--    <a href="#">发5</a>&nbsp;&nbsp;&nbsp;-->
        <!--    <a href="#">发30</a>-->
        <!--</td>-->
    </tr>

    </tbody>

</table>

<table class="table table-bordered table-hover definewidth m10">
    <tr>
        <th colspan="5">
            <div>
                <button type="button" class="btn btn-success" id="returnList">返回列表</button>
            </div>
        </th>
    </tr>
</table>
</body>
<script type="text/javascript">

    String.prototype.GetValue = function (para) {
        let reg = new RegExp("(^|&)" + para + "=([^&]*)(&|$)");
        let r = this.substr(this.indexOf("\?") + 1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }

    let url = document.location.toString();
    let pageNum = url.GetValue("pageNum");
    let behId = url.GetValue("behId");

    $(function () {

        //加载病人信息
        loadDate(behId);

        //返回
        $('#returnList').click(function () {
            window.location.href = "dispensing.html?pageNum=" + pageNum;
        });

    });

    // 加载信息
    function loadDate(behId) {
        $.ajax({
            type: "get",
            url: "/drugpeople/peopleInfo",
            data: {behId: behId},
            dataType: "json",
            success: function (vo) {
                if (vo.code !== 200){
                    alert(vo.msg);
                    return;
                }
                let list=vo.list;
                let str="";
                for(let i=0;i<list.length;i++){
                    let obj=list[i];
                    let behId = obj.behId;
                    let behName = obj.behName;
                    let drName = obj.drName;
                    let drNumber = obj.drNumber;
                    let drOknumber = obj.drOknumber;
                    let notSentDrugNum = obj.notSentDrugNum;
                    let drId = obj.drId;
                    str += 
                        "<tr> " +
                        "        <td style='vertical-align:middle;' name='behId'>"+behId+"</td> " +
                        "        <td style='vertical-align:middle;' name='behName'>"+behName+"</td> " +
                        "        <td style='vertical-align:middle;' name='drName'>"+drName+"</td> " +
                        "        <td style='vertical-align:middle;' name='drNumber'>"+drNumber+"</td> " +
                        "        <td style='vertical-align:middle;' name='drOknumber'>"+drOknumber+"</td> " +
                        "        <td style='vertical-align:middle;' name='notSentDrugNum'>"+notSentDrugNum+"</td> " +
                        "        <td style='vertical-align:middle;'> " +
                        "            <a href='#' onclick='sendDrug("+drId+",null)'>发全</a>&nbsp;&nbsp;&nbsp; " +
                        "            <a href='#' onclick='sendDrug("+drId+",1)'>发1</a>&nbsp;&nbsp;&nbsp; " +
                        "            <a href='#' onclick='sendDrug("+drId+",5)'>发5</a>&nbsp;&nbsp;&nbsp; " +
                        "            <a href='#' onclick='sendDrug("+drId+",30)'>发30</a> " +
                        "        </td> " +
                        "</tr>"
                }
                $("#tbody").html(str);
            }

        });
    }

    // 发药
    function sendDrug(drId,sendDrNumber){

        let behId = $("td[name='behId']").html();
        let drNames = $("td[name='drName']");
        let drName,tmp;
        for (let i = 0; i < drNames.length ; i++) {
            drName = $(drNames[i]).html();
            tmp = drName.substr(0,drName.lastIndexOf(" - "));
            if (drId === tmp){
                drName = $(drNames[i]).html();
                break;
            }
        }

        let num = sendDrNumber == null? "所有":sendDrNumber;
        let src = "确认发药《"+drName+"》"+num+"份";

        if(confirm(src)){
            $.ajax({
                type: "get",
                url: "/drugpeople/sendDrug",
                data: {
                    sendDrNumber: sendDrNumber,
                    behId: behId,
                    drId:drId
                },
                dataType: "json",
                success: function (vo) {
                    if (vo.code !== 200){
                        alert("发药失败！"+vo.msg);
                    }else{
                        alert("发药成功！");
                        window.location.href =  "dispensing-look.html?pageNum="+pageNum+"&behId="+behId;
                    }
                }
            });
        }


    }
</script>
</html>
